using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using CreamX.Framework.Game;
using CreamX.Framework.Game.Components;
using CreamX.Framework.Attributes;
using System.Drawing;
using CreamXStudio.Classes;

namespace CreamXStudio.Screens
{
    public class ObjectEditorHolder:EditorControlBase
    {
        public ObjectEditorHolder()
        {
            InitializeComponent();
            pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
        }

        void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (selobject is CreamXAnimatedObject)
            {
                CreamXAnimatedObject aaa = (CreamXAnimatedObject)selobject;
                Microsoft.Xna.Framework.Rectangle rect1 = aaa.GetSourcRectForFrame();
                Rectangle src = new Rectangle(rect1.X, rect1.Y, rect1.Width, rect1.Height);
                AnimationInfo an = ((CreamXAnimatedObject)selobject).Animation;
                e.Graphics.DrawImage(SessionData.Instance.ImageCache[selobject.Material.Name],
                    new Rectangle(0, 0, an.SizeX, an.SizeY), src, GraphicsUnit.Pixel);
            }
        }
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        internal ObjectEditor objectEditor1;
        private TabPage tabPage3;
        private PictureBox pictureBox1;
        private ComboBox comboBox1;
        private PropertyGrid propertyGrid1;
        private Button button1;
        private System.Windows.Forms.TabPage tabPage2;
    
        private void InitializeComponent()
        {
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.objectEditor1 = new CreamXStudio.Screens.ObjectEditor();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.button1 = new System.Windows.Forms.Button();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
            this.tabPage3 = new System.Windows.Forms.TabPage();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // tabControl1
            // 
            this.tabControl1.Appearance = System.Windows.Forms.TabAppearance.Buttons;
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Controls.Add(this.tabPage3);
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(0, 0);
            this.tabControl1.Multiline = true;
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(353, 609);
            this.tabControl1.TabIndex = 7;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.objectEditor1);
            this.tabPage1.Location = new System.Drawing.Point(4, 25);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(345, 580);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Properties";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // objectEditor1
            // 
            this.objectEditor1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.objectEditor1.Location = new System.Drawing.Point(3, 3);
            this.objectEditor1.Name = "objectEditor1";
            this.objectEditor1.SelectedObject = null;
            this.objectEditor1.Size = new System.Drawing.Size(339, 574);
            this.objectEditor1.TabIndex = 0;
            this.objectEditor1.Load += new System.EventHandler(this.objectEditor1_Load);
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.button1);
            this.tabPage2.Controls.Add(this.pictureBox1);
            this.tabPage2.Controls.Add(this.comboBox1);
            this.tabPage2.Controls.Add(this.propertyGrid1);
            this.tabPage2.Location = new System.Drawing.Point(4, 25);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(345, 580);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Animation";
            this.tabPage2.UseVisualStyleBackColor = true;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(259, 279);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(46, 23);
            this.button1.TabIndex = 3;
            this.button1.Text = "Play";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(3, 279);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(250, 229);
            this.pictureBox1.TabIndex = 2;
            this.pictureBox1.TabStop = false;
            // 
            // comboBox1
            // 
            this.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(6, 11);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(333, 21);
            this.comboBox1.TabIndex = 1;
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // 
            // propertyGrid1
            // 
            this.propertyGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.propertyGrid1.Location = new System.Drawing.Point(3, 38);
            this.propertyGrid1.Name = "propertyGrid1";
            this.propertyGrid1.Size = new System.Drawing.Size(336, 235);
            this.propertyGrid1.TabIndex = 0;
            // 
            // tabPage3
            // 
            this.tabPage3.Location = new System.Drawing.Point(4, 25);
            this.tabPage3.Name = "tabPage3";
            this.tabPage3.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage3.Size = new System.Drawing.Size(337, 618);
            this.tabPage3.TabIndex = 2;
            this.tabPage3.Text = "Animation";
            this.tabPage3.UseVisualStyleBackColor = true;
            // 
            // ObjectEditorHolder
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.Controls.Add(this.tabControl1);
            this.Name = "ObjectEditorHolder";
            this.Size = new System.Drawing.Size(353, 609);
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }
        CreamXObject selobject;
        internal override void SetData(object data)
        {
            base.SetData(data);
            objectEditor1.SelectedObject = data as CreamXObject;
            selobject = data as CreamXObject;
            if (selobject is CreamXAnimatedObject)
                LoadAnimations();
            //else
            //tabControl1.TabPages[0].CanSelect = false;
            AddComponents();
            objectEditor1.SetData(data);
        }
        private void LoadAnimations()
        {

            foreach (AnimationInfo ani in selobject.Scene.AnimationData)
            {
                comboBox1.Items.Add(ani.Name);

            }
            if (((CreamXAnimatedObject)selobject).Animation == null)
                return;
            if (((CreamXAnimatedObject)selobject).Animation.Name != string.Empty)
            {
                comboBox1.SelectedItem = ((CreamXAnimatedObject)selobject).Animation.Name;
                propertyGrid1.SelectedObject = ((CreamXAnimatedObject)selobject).Animation;
            }
        }

        private void AddComponents()
        {
            foreach (CreamXComponent com in selobject.Components)
            {
                object[] att = com.GetType().GetCustomAttributes(true);
                if (att.Length > 0)
                {
                    CreamXComponentAttribute a = att[0] as CreamXComponentAttribute;
                    TabPage tp = new TabPage();
                    tp.Text = a.FriendlyName;
                    tp.Tag = com;
                    PropertyGrid grid = new PropertyGrid();
                    tp.Controls.Add(grid);
                    grid.Dock = DockStyle.Fill;
                    grid.SelectedObject = com;
                    tabControl1.TabPages.Add(tp);
                }

            }
        }

        private void objectEditor1_Load(object sender, EventArgs e)
        {
            tmr.Tick += new EventHandler(tmr_Tick);
            tmr.Enabled = false;
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            ((CreamXAnimatedObject)selobject).Animation.CurrentFrame++;
            if (((CreamXAnimatedObject)selobject).Animation.CurrentFrame == ((CreamXAnimatedObject)selobject).Animation.EndFrame)
                ((CreamXAnimatedObject)selobject).Animation.CurrentFrame = ((CreamXAnimatedObject)selobject).Animation.StartFrame;

            pictureBox1.Invalidate();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ((CreamXAnimatedObject)selobject).Animation = selobject.Scene.GetAnimation(comboBox1.Text);
            propertyGrid1.SelectedObject = ((CreamXAnimatedObject)selobject).Animation;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!((CreamXAnimatedObject)selobject).Animation.IsRunning)
            {
                ((CreamXAnimatedObject)selobject).Animation.IsRunning = true;
                int xx = ((CreamXAnimatedObject)selobject).Animation.EndFrame -( ((CreamXAnimatedObject)selobject).Animation.StartFrame+1);
                float y = (float)xx / (float)((CreamXAnimatedObject)selobject).Animation.AnimationSpeed;
               if(y<0.5f) y=1;
                
                tmr.Interval = (int)y*10;
                tmr.Enabled = true;
            }
            else
            {
                ((CreamXAnimatedObject)selobject).Animation.IsRunning = false;
                tmr.Enabled = false;
            }
        }
        Timer tmr = new Timer();
    }
}
